package com.zerowidth.moc.web.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间戳转换工具类
 * 注：所有方法入参都是时间戳，时间戳的单位统一使用 毫秒
 * Created by meikai on 2019/07/01.
 */
public class TimeUtil {

    public static final String TIME_FORMAT_YYYY_MM_DD_1 = "yyyy.MM.dd";
    public static final String TIME_FORMAT_YYYY_MM_DD_2 = "yyyy-MM-dd";

    public static final String TIME_FORMAT_MM_DD_1 = "MM-dd";

    public static final String TIME_FORMAT_HH = "HH";

    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
    public static final String TIME_FORMAT_YYYY_MM_DD_HH_MM_SS_2 = "yyyy年MM月dd日 HH:mm:ss";

    public static final String TIME_FORMAT_MM_DD_HH_MM = "MM月dd日 HH:mm";

    public static final String TIME_FORMAT_HH_MM = "HH:mm";
    public static final String TIME_FORMAT_HH_MM_SS = "HH:mm:ss";

    /**
     * @param timeStamp 时间戳，单位：毫秒
     * @param format    转换的格式，形如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String convert(long timeStamp, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(new Date(timeStamp));

        } catch (Exception e) {
        }
        return "";
    }

    public static long convert(String timeStampStr, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(timeStampStr).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String now() {
        return convertYYYYMMDDHHMMSS(System.currentTimeMillis());
    }

    public static String now2() {
        return convertYYYYMMDDHHMMSSSSS(System.currentTimeMillis());
    }

    public static String convertYYYYMMDD(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_1);
    }

    public static String convertYYYYMMDDHHMMSS(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);
    }

    public static String convertYYYYMMDDHHMMSSSSS(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_HH_MM_SS_SSS);
    }

    public static String convertYYYYMMDD2(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2);
    }

    /**
     * 获取 24小时制下的  HH:mm
     */
    public static String convertHHMM_24(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_HH_MM);
    }

    /**
     * 获取 12小时制下的  HH:mm
     */
    public static String convertHHMM_12(long timeStamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp);

        int hour12JDK = calendar.get(Calendar.HOUR); //jdk 12小时制的 hour值域=[0-11]
        int hour12ui = hour12JDK;  //但在ui显示上，jdk hour=0应该显示为 12
        if (hour12JDK == 0) {
            hour12ui = 12;
        }

        return hour12ui + ":" + String.format("%02d", calendar.get(Calendar.MINUTE));
    }

    public static String convertMMDDHHMM(long timeStamp) {
        return convert(timeStamp, TIME_FORMAT_MM_DD_HH_MM);
    }

    public static String getTodayStart() {
        return convert(System.currentTimeMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getTodayEnd() {
        return convert(System.currentTimeMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 23:59:59";
    }

    public static String getYesterdayStart() {
        long timeStamp = System.currentTimeMillis();
        timeStamp -= 24 * 60 * 60 * 1000;
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getYesterdayEnd() {
        long timeStamp = System.currentTimeMillis();
        timeStamp -= 24 * 60 * 60 * 1000;
        return convert(timeStamp, TIME_FORMAT_YYYY_MM_DD_2) + " 23:59:59";
    }

    public static String getMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getMonthEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getLastMonthStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    public static String getLastMonthEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return convert(calendar.getTimeInMillis(), TIME_FORMAT_YYYY_MM_DD_2) + " 00:00:00";
    }

    /**
     * 判断时间是否为今天
     *
     * @param time 单位：毫秒
     */
    public static boolean isToday(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH);
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramMonth = calendar.get(Calendar.MONTH);
        int paramDay = calendar.get(Calendar.DAY_OF_MONTH);

        return currentYear == paramYear && currentMonth == paramMonth && currentDay == paramDay;
    }

    /**
     * 判断时间是否为昨天
     *
     * @param time 单位：毫秒
     */
    public static boolean isYesterday(long time) {
        return isYesterday(time, 1);
    }

    /**
     * 判断时间是否为前天
     *
     * @param time 单位：毫秒
     */
    public static boolean isYesterday2(long time) {
        return isYesterday(time, 2);
    }

    /**
     * 判断时间是否为 N 个昨天
     * N = 1 : 昨天
     * N = 2 : 前天
     * N = 3 : 大前天
     * 以此类推
     *
     * @param time 单位：毫秒
     */
    public static boolean isYesterday(long time, int yesterCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -yesterCount);

        int yesterdayYear = calendar.get(Calendar.YEAR);
        int yesterdayMonth = calendar.get(Calendar.MONTH);
        int yesterdayDay = calendar.get(Calendar.DAY_OF_MONTH);

        Calendar calendarParam = Calendar.getInstance();
        calendarParam.setTimeInMillis(time);
        int paramYear = calendarParam.get(Calendar.YEAR);
        int paramMonth = calendarParam.get(Calendar.MONTH);
        int paramDay = calendarParam.get(Calendar.DAY_OF_MONTH);

        return yesterdayYear == paramYear && yesterdayMonth == paramMonth && yesterdayDay == paramDay;
    }

    /**
     * 判断时间是否为本周
     *
     * @param time 单位：毫秒
     */
    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);

        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);

        return currentYear == paramYear && currentWeek == paramWeek;
    }

    /**
     * 判断时间是否为下周
     *
     * @param time 单位：毫秒
     */
    public static boolean isNextWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);

        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);

        return currentYear == paramYear && paramWeek == currentWeek + 1;
    }

    /**
     * 判断是否为工作时间，周一至周四、周五白天
     */
    public static boolean isWorkTime() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (day >= 0 && day <= 3) {
            return true;
        }
        if (day == 4 && hour < 18) {
            return true;
        }

        return false;
    }

    /**
     * 判断是否为快乐时间，周五晚上
     */
    public static boolean isHappyTime() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (day == 4 && hour >= 18) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否为休息时间，周六日全天
     */
    public static boolean isRestTime() {

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (day == 5 || day == 6) {
            return true;
        }
        return false;
    }

    /**
     * 判断时间是否为今年
     *
     * @param time 单位：毫秒
     */
    public static boolean isThisYear(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);

        calendar.setTime(new Date(time));
        int paramYear = calendar.get(Calendar.YEAR);
        return currentYear == paramYear;
    }

    public static int getCurrentHour() {
        String hourStr = convert(System.currentTimeMillis(), TIME_FORMAT_HH);
        return Integer.parseInt(hourStr);
    }


    /**
     * 两个时间点相减，用于判断时间的先后关系
     * @return 负数：1时间小于2时间；正数：1时间大于2时间
     */
    public static long timeSubtraction(String time1, String time2) {
        return TimeParseUtil.parseYYYYMMDDHHmmss(time1).getTimeInMillis()
                - TimeParseUtil.parseYYYYMMDDHHmmss(time2).getTimeInMillis();
    }

    /**
     * 计算参数时间戳已经流逝过去多少秒
     *
     * 备注：当前时间戳减参数时间，单位：秒
     *
     * @param dateTimeStr 时间格式："yyyy-MM-dd HH:mm:ss"
     */
    public static long timePass(String dateTimeStr) {
        long diff = System.currentTimeMillis() - TimeParseUtil.parseYYYYMMDDHHmmss(dateTimeStr).getTimeInMillis();

        return diff / 1000;
    }

}